package vip.xiaonuo.bi.modular.dataset.dispatcher;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.bi.core.datasource.util.CSVutils;
import vip.xiaonuo.bi.core.datasource.util.DButils;
import vip.xiaonuo.bi.modular.dataset.param.DataSetCsvTypeParam;
import vip.xiaonuo.bi.modular.dataset.param.DataSetTypeParam;
import vip.xiaonuo.bi.modular.dataset.param.DatasetInfo;

import java.util.List;

/**
 * @auther: 顾育司
 * @Description
 * @date: 2024/1/13
 */
@Component
public class FileCSVDispatcher extends DataSetClient{


    @Override
    public String getDataSetType() {
        return "file_csv";
    }

    @Override
    public List<List<String>> getData(DataSetTypeParam dataSetTypeParam) {
        DataSetCsvTypeParam dataSetCsvTypeParam = (DataSetCsvTypeParam) dataSetTypeParam;
        MultipartFile file = dataSetCsvTypeParam.getFile();
        String charset = dataSetCsvTypeParam.getCharset();
        String colInfoType = dataSetCsvTypeParam.getColInfoType();
        return CSVutils.uploadCsv(file,charset,colInfoType).getData();
    }


    @Override
    public DatasetInfo getCount(DataSetTypeParam dataSetTypeParam) {
        // 定义结果集
        DatasetInfo datasetInfo = new DatasetInfo();

        DataSetCsvTypeParam dataSetCsvTypeParam = (DataSetCsvTypeParam) dataSetTypeParam;

        // 构建查询总条数sql
        // 构建查询行数sql
        // 构建查询数据量大小sql
        List<List<String>>  colResult = dorisTbService.exeQuerySql1(DButils.getQueryColumnSql(dataSetCsvTypeParam.getTable()));
        List<List<String>>  tableResult = dorisTbService.exeQuerySql1(DButils.getQueryCountByTable(dataSetCsvTypeParam.getTable()));
        List<List<String>>  sizeResult = dorisTbService.exeQuerySql1(DButils.getQueryDataSizeByTable(dataSetCsvTypeParam.getTable()));

        // 结果解析
        datasetInfo.setColumnCount(colResult.get(0).size());
        datasetInfo.setRowCount(new Integer(tableResult.get(1).get(0)));
        datasetInfo.setDataSize(new Long(sizeResult.get(1).get(0)));

        return datasetInfo;
    }
}
